package com.cn.webspider;

import org.apache.commons.lang3.StringUtils;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

public class DownloadPicture implements Pipeline {
    private int count = 1;

    @Override
    public void process(ResultItems resultItems, Task task) {
        String picUrl = resultItems.get("picUrl");
        String picName = resultItems.get("picName");
        String dirc = "E:\\spiderPicture\\" + picName + "\\";
        if (StringUtils.isAnyBlank(picName, picUrl)) {
            return;
        } else {
            URL url = null;
            DataInputStream dataInputStream = null;
            FileOutputStream fileOutputStream = null;
            try {
                File file = new File(dirc);
                if (!file.exists()) {//如果文件夹不存在
                    file.mkdir();//创建文件夹
                }
                url = new URL(picUrl);
                dataInputStream = new DataInputStream(url.openStream());
                fileOutputStream = new FileOutputStream(new File(dirc + count + picUrl.substring(picUrl.lastIndexOf("."))))
                ;
                byte[] bytes = new byte[1024 * 1024];
                int length;
                while ((length = dataInputStream.read(bytes)) > 0) {
                    fileOutputStream.write(bytes, 0, length);
                }
                System.out.println("已下载" + count++ + "张" + "," + picName);
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    dataInputStream.close();
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }
    }
}
